package cn.wolfcode.wolf2w.controller;

import cn.wolfcode.wolf2w.annotation.RequireLogin;
import cn.wolfcode.wolf2w.annotation.UserParam;
import cn.wolfcode.wolf2w.domain.Strategy;
import cn.wolfcode.wolf2w.domain.StrategyComment;
import cn.wolfcode.wolf2w.domain.UserInfo;
import cn.wolfcode.wolf2w.query.StrategyCommentQuery;
import cn.wolfcode.wolf2w.query.StrategyQuery;
import cn.wolfcode.wolf2w.redis.service.IStrategyStatisVORedisService;
import cn.wolfcode.wolf2w.service.IStrategyCommentService;
import cn.wolfcode.wolf2w.service.IStrategyService;
import cn.wolfcode.wolf2w.service.IStrategyThemeService;
import cn.wolfcode.wolf2w.util.JsonResult;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/strategies")
public class StrategieController {
    @Autowired
    private IStrategyService strategyService;
    @Autowired
    private IStrategyThemeService strategyThemeService;
    @Autowired
    private IStrategyCommentService strategyCommentService;
    @Autowired
    private IStrategyStatisVORedisService strategyStatisVORedisService;

    @GetMapping("/detail")
    public Object detail(String id){
        //阅读数+1
        strategyStatisVORedisService.increaseNumview(id,1);
       return JsonResult.success(strategyService.get(id));
    }

    @GetMapping("/query")
    public Object query(StrategyQuery qo){
        return JsonResult.success(strategyService.query(qo));
    }

    @GetMapping("/themes")
    public Object themes(){
        return JsonResult.success(strategyThemeService.list());
    }


    @RequireLogin
    @PostMapping("/addComment")
    public Object addComment(StrategyComment comment, @UserParam UserInfo UserInfo){
        //设置用户相关信息
        //用spring的BeanUtils.copyPropertiees(userInfo,comment)
        //参数1：源对象（ctrl+c的位置）参数2：目标对象（ctrl_+v的位置）
        BeanUtils.copyProperties(UserInfo,comment);
        comment.setUserId(UserInfo.getId());

       strategyCommentService.save(comment);
        strategyStatisVORedisService.increaseReplynum(comment.getStrategyId(),1);
        return JsonResult.success();
    }

    @GetMapping("/statisVo")
    public Object statisVo(String sid){
        return JsonResult.success(strategyStatisVORedisService.getStrategyStaticVo(sid));
    }

    @GetMapping("/comments")
    public Object comments(StrategyCommentQuery qo){
        return JsonResult.success(strategyCommentService.query(qo));
    }

    @RequireLogin
    @PostMapping("/commentThumb")
    public Object commentThumb(String cid,@UserParam UserInfo userInfo){
        strategyCommentService.commentThumb(cid,userInfo.getId());
        return JsonResult.success();
    }

    @RequireLogin
    @PostMapping("/favor")
    public Object favor(String sid,@UserParam UserInfo userInfo){
        boolean ret = strategyStatisVORedisService.favor(sid,userInfo.getId());
        return JsonResult.success(ret);
    }

    @RequireLogin
    @PostMapping("/strategyThumbup")
    public Object strategyThumbup(String sid,@UserParam UserInfo userInfo){
        boolean ret = strategyStatisVORedisService.strategyThumbup(sid,userInfo.getId());
        return JsonResult.success(ret);
    }

}
